<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/6/14
 * Time: 1:17 PM
 */
namespace Admin\Controller;
use Admin\Model;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ArrayObject;
use Zend\View\Model\ViewModel;
use Admin\Model\AdminCategories;
use Velacolib\Utility\Utility;
class CategoriesController extends AbstractActionController
{
    protected $CategoriesTable;
    protected $service_locator_str;
    protected $auth;
    protected $TopicTable;

    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        $this->service_locator_str = 'Admin\Model\AdminCategoriesTable';
        $this->sm = $this->getServiceLocator();
        $this->CategoriesTable = $this->sm->get($this->service_locator_str);

        $this->service_locator_str = 'Admin\Model\AdminTopicTable';
        $this->sm = $this->getServiceLocator();
        $this->TopicTable = $this->sm->get($this->service_locator_str);

        //$user =  Utility::CheckLogin($this);
        $this->auth = Utility::CheckLogin($this);
        if ($this->auth!=null && $this->auth->user_type != 3 ) {
            $this->redirect()->toRoute('admin/child',array('controller'=>'login'));
        }
        if ($this->auth==null){
            $this->redirect()->toRoute('admin/child',array('controller'=>'login'));
        }
        return parent::onDispatch($e);

    }

    public function init(){

    }



    public function indexAction()
    {
       /* if(!$this->CategoriesTable){
            $sm = $this->getServiceLocator();
            $this->CategoriesTable = $sm->get('Admin\Model\AdminCategoriesTable');
        }*/


        $paging = $this->CategoriesTable->fetchAll(true);
        $paging->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
        // set the number of items per page to 10
        $paging->setItemCountPerPage(500);

        $category = $this->CategoriesTable->fetchAll();

        $topic_cat = $this->CategoriesTable->fetchAll(true);

        return new ViewModel( array(
            'topic_cat' => $paging ,
            'category_option'=>$category,
            'top_table'=>$this->TopicTable,
        ));

    }


    public function addAction(){

        $request = $this->getRequest();
        if ($request->isPost()) {

            $category['title'] = $this->getRequest()->getPost('title');
            $category['cat_parent'] = $this->getRequest()->getPost('cat_parent');
            $category['sort_order'] = $this->getRequest()->getPost('sort_order');
            $category_model = new Model\AdminCategories();
            $category_model->exchangeArray($category);
            $this->CategoriesTable->saveCategories($category_model);

        }
        $category = $this->CategoriesTable->fetchAll();
        return new ViewModel(array('cate'=>$category));
    }


    public function deleteAction(){


        $dataEdit = new ArrayObject();
        $cate_id = $this->getRequest()->getPost('del_id');
        $diffParentId = $this->getRequest()->getPost('cat_parent');
        if($cate_id != 0){
            if($diffParentId != 0){
                $subcate = $this->CategoriesTable->getCategoryBy($cate_id);
                $dataEdit->cat_parent = $diffParentId;
                foreach($subcate as $key=>$val){
                    $dd[] = $val;
                }
                foreach($dd as $v){
                    $dataEdit->id = $v->id;
                    $this->CategoriesTable->saveCategories($dataEdit);
                }
                $this->CategoriesTable->deleteCategories($cate_id);
            }else{
                $subcate = $this->CategoriesTable->getCategoryBy($cate_id);
                if(count($subcate) >0){
                foreach($subcate as $key=>$val){
                    $dd[] = $val;
                }

                foreach($dd as $v){
                    $id = $v->id;
                    $this->CategoriesTable->deleteCategories($id);
                }
                }
                $this->CategoriesTable->deleteCategories($cate_id);
            }

            return $this->redirect()->toRoute('admin/child',array(
                'controller'=>'categories',
            ));
        }
    }



    public function editAction(){

        $cateId = $this->params()->fromRoute('id',0);
        if (!$cateId) {
            return $this->redirect()->toRoute('categories', array(
                'action' => 'add'
            ));
        }
        $cateEdit = $this->CategoriesTable->getCategories($cateId);
        $category = $this->CategoriesTable->fetchAll();

        $request = $this->getRequest();
        if ($request->isPost()) {

            $dataEdit['title'] = $this->getRequest()->getPost('title');
            $dataEdit['cat_parent'] = $this->getRequest()->getPost('parent_cat');
            $dataEdit['sort_order'] = $this->getRequest()->getPost('sort_order');
            $dataEdit['id'] = $cateId;
            $category_model = new Model\AdminCategories();
            $category_model->exchangeArray($dataEdit);
            $this->CategoriesTable->saveCategories($category_model);
            return $this->redirect()->toRoute('admin/child',array(
                'controller'=>'categories',
                'action'=>'edit',
                'id'=>$cateId
            ));

        }

        return new ViewModel(array(
            'cateEdit'=>$cateEdit,
            'cate' => $category,
        ));


    }

    public function toArray() {
        $vars = get_object_vars ( $this );
        $array = array ();
        foreach ( $vars as $key => $value ) {
            $array [ltrim ( $key, '_' )] = $value;
        }
        return $array;
    }
    public function exampledataAction(){



    }
}